<?php
namespace app\index\controller;

use think\Controller;
use think\View;
use Wechat\TPWechat;

class Common extends Controller {

    protected $_action;
    protected $_controller;
    protected $wechatObj;

    public function _initialize() {
        if (!is_weixin()) {
            $user = input('param.user');
            session('username', $user);
            switch ($user) {
                case 'popo';
                    session('userid', 1);
                    break;
                case 'lsl';
                    session('userid', 2);
                    break;
            }
        } else {

            if (!session('?openid') && request()->action() != 'baseweixinlogin') {
                if (!input('param.base_model')) {
                    session('base_url', request()->controller() . '/' . request()->action());
                    session('base_param', input('param.'));
                }

                $this->oauth(); // 发起授权
            }
        }
    }

    protected function getWechtObj()
    {
        if ($this->wechatObj == null)
        {
            $options = config('wechat');
            $this->wechatObj = new TPWechat($options);
        }
        return $this->wechatObj;
    }

    // 微信回调页面
    public function baseweixinlogin()
    {
        $code = isset($_GET['code']) ? $_GET['code'] : '';
        if (!$code)
            $this->error('错误页面1');

        $oauthAccessToken = $this->getWechtObj()->getOauthAccessToken();
        //dump($oauthAccessToken);

        if (!$oauthAccessToken)
            $this->error('获取授权失败');


        //dump($userInfo);exit;
        if (!session('?model')) {
            session('model', null);
            $userInfo = $this->getWechtObj()->getUserinfo($oauthAccessToken['openid']);
            if (isset($userInfo['subscribe']) && $userInfo['subscribe'] == 0) {
                if (session('check_subscribe') == 1) {
                    session('check_subscribe_val', null);
                } else {
                    $param = session('base_param'); // 未关注的时候记录参数  用于关注后的业务处理
                    $this->redirect(url('base/subscribe'));
                }
            }
        } else {
            $userInfo = $this->getWechtObj()->getOauthUserinfo($oauthAccessToken['openid']);
        }

        if (!$userInfo) {
            unlink('./../runtime/access_token.json'); // 删除access_token 文件
            $this->oauth(); // 跳转重新发起授权请求
        }

        // 授权成功, 记录微信信息
        session('openid', $userInfo['openid']);
        session('nickname', $userInfo['nickname']);
        session('headimgurl', $userInfo['headimgurl']);

        //dump(session(''));exit;
        if (!session('?base_url')) {
            $this->redirect('index/index');
        } else {
            $base_param = session('base_param');
            $base_param['oauth'] = 'yes';
            $this->redirect(session('base_url'), $base_param);
        }
        return;
    }

    // 微信授权方法
    public function oauth()
    {
        $url = request()->domain() . url('index/common/baseweixinlogin');
        $url = $this->getWechtObj()->getOauthRedirect($url);
        exit('<script>window.location.href="' . $url . '"</script>');
    }

    /**
     * 本地调用接口方法
     * @param $action
     * @param array $data
     * @return array|mixed|\think\response\Json
     */
    public function local_invoke_api($action, $data = []) {
        $return = invoke_api($action, $data);
        if (!request()->isAjax()) {  // 不是ajax请求的时候检查数据获取是否正确， 发生错误则加载错误页面
            if ($return['code'] != 200) {
                $this->assign('msg', $return['msg']); // 输出错误信息
                $this->error($return['msg']);
                return view('public/error');  // 输出错误页面
            }
        }
        return $return;
    }
}
